package com.ruoyi.algorithm.controller;

import com.ruoyi.algorithm.entity.Operator;
import com.ruoyi.algorithm.entity.OperatorVersion;
import com.ruoyi.algorithm.model.ImageModel;
import com.ruoyi.algorithm.service.ImageService;
import com.ruoyi.algorithm.service.OperatorService;
import com.ruoyi.algorithm.service.OperatorVersionService;
import com.beihu.sightplus.common.response.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：yanfa-03
 * @date ：Created in 2021/7/13/10:21
 * @description ：算子版本控制器
 * @version: 1.0
 */
@RestController
@RequestMapping("operatorVersion")
@Slf4j
@Api(tags = "算子版本控制器")
public class OperatorVersionController {

    @Autowired
    private OperatorVersionService operatorVersionService;

    @Autowired
    private ImageService imageService;

    @Autowired
    private OperatorService operatorService;

    @GetMapping("getByOperatorId")
    @ApiOperation("根据算子ID获取算子版本列表")
    public ResultData<List<OperatorVersion>> getByOperatorId(@ApiParam("算子ID") @RequestParam("operatorId") Long operatorId) {
        List<OperatorVersion> byOperatorId = operatorVersionService.findByOperatorId(operatorId);
        return ResultData.success(byOperatorId);
    }

    @PutMapping("putOperatorVersion")
    @ApiOperation("新增算子版本")
    public ResultData<?> putOperatorVersion(@RequestBody OperatorVersion operatorVersion) {
        try {
            operatorVersionService.addOperatorVersion(operatorVersion);
            return ResultData.success();
        } catch (RuntimeException e) {
            log.info(e.toString());
            return new ResultData<>(403, e.getMessage(), null);
        }
    }

    @GetMapping("getImageDropDownBox")
    @ApiOperation("获取镜像版本与版本号下拉框")
    public ResultData<List<ImageModel>> getImageDropDownBox() {
        List<ImageModel> result = imageService.findImageDropDownBox();
        return ResultData.success(result);
    }

    @PostMapping("editOperatorVersion")
    @ApiOperation("编辑算子版本")
    public ResultData<?> editOperatorVersion(@RequestBody OperatorVersion operatorVersion) {
        if (operatorVersionService.editOperatorVersion(operatorVersion)) {
            return ResultData.success();
        } else return new ResultData<>(200, "更新了0条数据", null);
    }

    @DeleteMapping("deleteOperatorVersion")
    @ApiOperation("删除算子版本")
    public ResultData<?> deleteOperatorVersion(@ApiParam("算子版本ID") @RequestParam("id") Long id) {
        Operator operator = operatorService.findByOperatorVersion(id);
        if (!ObjectUtils.isEmpty(operator)) {
            return new ResultData<>(403, "删除失败，该算子版本为当前算法使用版本", null);
        }
        if (operatorVersionService.deleteById(id)) {
            return ResultData.success();
        } else {
            return new ResultData<>(200, "删除了0条数据", null);
        }
    }

    @GetMapping("getById")
    @ApiOperation("根据ID查询算子版本")
    public ResultData<OperatorVersion> getByid(@ApiParam("算子版本ID") @RequestParam("id") Long id) {
        OperatorVersion operatorVersion = operatorVersionService.findByid(id);
        return ResultData.success(operatorVersion);
    }

    @GetMapping("isVersionExist")
    @ApiOperation("算子版本是否不存在")
    @ApiResponse(code = 200, message = "true:不存在,false:已存在")
    public ResultData<?> isVersionExist(@ApiParam("算子ID") @RequestParam Long operatorId,
                                        @ApiParam("算子版本号") @RequestParam String version) {
        boolean notExist = operatorVersionService.isVersionExist(operatorId, version);
        return ResultData.success(notExist);
    }

    @GetMapping("getByVersion")
    @ApiOperation("根据算子ID与版本号查算子版本信息")
    public ResultData<OperatorVersion> getByVersion(@ApiParam("算子ID") @RequestParam Long operatorId,
                                                    @ApiParam("算子版本号") @RequestParam String version) {
        OperatorVersion operatorVersion = operatorVersionService.findbyVersion(operatorId, version);
        return ResultData.success(operatorVersion);
    }

    @GetMapping("getAlreadyDel")
    @ApiOperation("查询已删除的算子版本列表")
    public ResultData<List<OperatorVersion>> getAlreadyDel() {
        List<OperatorVersion> operatorVersions = operatorVersionService.findAlreadyDel();
        return ResultData.success(operatorVersions);
    }

    @PostMapping("recoverById")
    @ApiOperation("恢复历史算子版本")
    public ResultData<?> recoverById(@RequestBody @ApiParam("算子版本ID") Long id) {
        try {
            operatorVersionService.recoverByid(id);
        } catch (RuntimeException e) {
            return new ResultData<>(400, "恢复失败", e.getMessage());
        }
        return ResultData.success("恢复成功");
    }
}
